package controller;

import view.IFormView;
import model.IRepository;
import model.User;

public class UserFormController implements IFormController<User> {

	protected IRepository<User> repo;
	protected IFormView<User> view;

	@Override
	public void submitRequest(String[] values) {
		if(values.length == 2){
			User newUser = new User(values[0], values[1]);
			repo.save(newUser);
			view.render(newUser);
		}else{
			view.render("Usuario y contraseña son requeridos para crear un usuario nuevo");
		}
	}
	
	@Override
	public void searchRequest(String value) {
		Integer id = Integer.valueOf(value);
		User user = repo.find(id);
		if(user != null){
			view.render(user);
		}else{
			view.render("Usuario inexistente!");
		}
	}

	public IRepository<User> getRepo() {
		return repo;
	}
	
	public void setRepo(IRepository<User> repo) {
		this.repo = repo;
	}
	
	public IFormView<User> getView() {
		return view;
	}
	
	public void setView(IFormView<User> view) {
		this.view = view;
	}
	
}
